package com.xcity.game.item;

import java.text.MessageFormat;
import java.util.Arrays;

import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.item.drop.WeightDropGroup;
import com.xcity.game.item.drop.impl.GoldDrop;
import com.xcity.game.item.drop.impl.ItemDrop;
import com.xcity.game.item.drop.impl.StoneDrop;

import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;

/**
 * 掉落组
 * @author yang.li
 *
 */
@Excel(value = "item.xlsx", sheet = 1)
public class DropGroupTemplate extends Template {

	/** 掉落现金 */
	protected int minGold, maxGold;
	
	/** 掉落钻石 */
	protected int minStone, maxStone;
	
	/** 最少掉落物品数 */
	protected int minNum;

	/** 最多掉落物品数 */
	protected int maxNum;

	/** 掉落物品[物品id, 最少数量, 最多数量, 权重] */
	protected int[] items;
	protected WeightDropGroup drop;
	
	public int getMinGold() {
		return minGold;
	}

	public void setMinGold(int minGold) {
		this.minGold = minGold;
	}

	public int getMaxGold() {
		return maxGold;
	}

	public void setMaxGold(int maxGold) {
		this.maxGold = maxGold;
	}

	public int getMinStone() {
		return minStone;
	}

	public void setMinStone(int minStone) {
		this.minStone = minStone;
	}

	public int getMaxStone() {
		return maxStone;
	}

	public void setMaxStone(int maxStone) {
		this.maxStone = maxStone;
	}

	public int getMinNum() {
		return minNum;
	}

	public void setMinNum(int minNum) {
		this.minNum = minNum;
	}

	public int getMaxNum() {
		return maxNum;
	}

	public void setMaxNum(int maxNum) {
		this.maxNum = maxNum;
	}

	public int[] getItems() {
		return items;
	}

	public void setItems(int[] items) {
		this.items = items;
	}
	
	public WeightDropGroup getDrop() {
		return drop;
	}

	@Override
	public void validate() throws TemplateException {
		if (/*maxNum < 0 || */minNum > maxNum) {
			throw new TemplateException(MessageFormat.format("掉落组配置错误: id={0,number,#}, minNum={1,number,#}, maxNum={2,number,#}", id, minNum, maxNum));
		}
		drop = new WeightDropGroup(minNum, maxNum);
		if (maxGold > 0) {
			if (minGold > maxGold || minGold < 0) {
				throw new TemplateException(MessageFormat.format("掉落组配置错误: id={0,number,#}, minGold={1,number,#}, maxGold={2,number,#}", id, minGold, maxGold));
			}
			drop.addDrop(new GoldDrop(GameStaticConfig.defaultChanceBase, 0, minGold, maxGold));
		}
		if (maxStone > 0) {
			if (minStone > maxStone || minStone < 0) {
				throw new TemplateException(MessageFormat.format("掉落组配置错误: id={0,number,#}, minStone={1,number,#}, maxStone={2,number,#}", id, minStone, maxStone));
			}
			drop.addDrop(new StoneDrop(GameStaticConfig.defaultChanceBase, 0, minStone, maxStone));
		}
//		物品id, 最少数量, 最多数量, 权重
		int len = items.length;
		if ((len & 3) != 0) { // len % 4
			throw new TemplateException(MessageFormat.format("掉落组配置错误: id={0,number,#}, items={1}", id, Arrays.toString(items)));
		}
		for (int i = 0; i < len; i += 4) {
			int id = items[i];
			int min = items[i + 1];
			int max = items[i + 2];
			int weight = items[i + 3];
			if (/*max < 0 || */min > max) {
				throw new TemplateException(MessageFormat.format("掉落组配置错误: id={0,number,#}, 物品{1,number,#}={2,number,#},{3,number,#},{4,number,#},{5,number,#},", id, i / 4 + 1, id, min, max, weight));
			}
			ItemDrop d = new ItemDrop(GameStaticConfig.defaultChanceBase, weight, id, min, max);
			drop.addDrop(d);
		}
		items = null;
	}
}
